package hillfly.wifichat.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.TrafficStats;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hillfly.wifichat.R;
import hillfly.wifichat.common.BaseActivity;

/**
 * Created by Administrator on 2017/7/18.
 */

public class TrafficstatsActivity extends BaseActivity implements View.OnClickListener {
    Button middleBt;
    TextView packetTv;
    Dialog dialog;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(1 == msg.what) {
                String flow = (String)msg.obj;
                packetTv.setText(flow);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trafficstats);
        initViews();
        initEvents();
    }

    @Override
    protected void initViews() {
        middleBt = (Button)findViewById(R.id.middle);
        packetTv = (TextView)findViewById(R.id.packet);
        flow();
    }

    private void flow() {
        PackageManager manager = mContext.getPackageManager();
        ArrayList<PackageInfo> appInfoList = getAllApps();
        for (PackageInfo info : appInfoList) {
            ApplicationInfo applicationInfo = info.applicationInfo;
            String pkg = info.packageName;
            String app = manager.getApplicationLabel(applicationInfo)
                    .toString();
            int uid = info.applicationInfo.uid;
            long rx = TrafficStats.getUidRxBytes(uid);
            long tx = TrafficStats.getUidTxBytes(uid);

            Log.i("tag",
                    "-----------------------uid=" + uid + "--rx=" + rx
                            + "--tx=" + tx + "---rx1="
                            + TrafficStats.getTotalRxBytes()/1024 + "---tx1="
                            + TrafficStats.getTotalTxBytes()/1024);
            String str =  "-----------------------uid=" + uid + "--rx=" + rx
                    + "--tx=" + tx + "---rx1="
                    + TrafficStats.getTotalRxBytes()/1024 + "---tx1="
                    + TrafficStats.getTotalTxBytes()/1024;
            if(null != handler){
                Message msg = new Message();
                msg.what = 1;
                msg.obj = str;
                handler.sendMessage(msg);
            }
        }

    }

    public ArrayList<PackageInfo> getAllApps() {
        ArrayList<PackageInfo> appList = new ArrayList<PackageInfo>();
        PackageManager pm = mContext.getPackageManager();
        List<PackageInfo> appList_temp = pm.getInstalledPackages(0);
        for (int i = 0; i < appList_temp.size(); i++) {
            PackageInfo pak = appList_temp.get(i);
            // 过滤系统的程序
            // if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <=
            // 0) {
            // appList.add(pak);
            // }
            if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
                appList.add(pak);
            }
        }
        appList_temp.clear();
        return appList;
    }

    @Override
    protected void initEvents() {
        middleBt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.middle){
            showDialog();
        }
    }

    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("列表对话框");
//        builder.setIcon(R.drawable.dialog);
        DialogInterface.OnClickListener listener =
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface,
                                        int which) {
//                        EditText editText =
//                                (EditText) findViewById(R.id.editText);
//                        editText.setText("你选择了: " +getResources()
//                                .getStringArray(R.array.hobby)[which]);
                    }
                };
        builder.setItems(R.array.name_array, listener);
        dialog = builder.create();
        dialog.show();
    }

//    @Override
//    public Dialog onCreateDialog(Bundle savedInstanceState) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(StartActivity.this);
//        builder.setTitle("UserName")
//                .setItems(R.array.name_array, new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//                        // The 'which' argument contains the index position
//                        // of the selected item
//                    }
//                });
//        return builder.create();
//    }
}
